<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>获取不匹配遮罩的起始子字符串的长度</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.strcoll.html">« strcoll</a></li>
      <li style="float: right;"><a href="function.strip-tags.html">strip_tags »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.strings.html">字符串 函数</a></li>
    <li>获取不匹配遮罩的起始子字符串的长度</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.strcspn" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">strcspn</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">strcspn</span> &mdash; <span class="dc-title">获取不匹配遮罩的起始子字符串的长度</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.strcspn-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>strcspn</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$characters</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code><span class="initializer"> = 0</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">int</span><span class="type"></span></span> <code class="parameter">$length</code><span class="initializer"> = <strong><code>null</code></strong></span></span><br>): <span class="type">int</span></div>

  <p class="para rdfs-comment">
   返回 <code class="parameter">string</code> 中，所有字符都<em class="emphasis">不</em>存在于 <code class="parameter">characters</code> 范围的起始子字符串的长度。
  </p>
  <p class="para">
   如果省略 <code class="parameter">offset</code> 和 <code class="parameter">length</code>，则将检查所有的
   <code class="parameter">string</code>。如果包含前面两个参数，那么跟调用 <code class="literal">strcspn(substr($string, $offset, $length),
   $characters)</code> 效果相同（参阅 <a href="function.substr.html" class="xref">substr</a> 获取更新信息）。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.strcspn-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">string</code></dt>

     <dd>

      <p class="para">
       要检查的字符串。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">characters</code></dt>

     <dd>

      <p class="para">
       包含每个不允许的字符的字符串。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">offset</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">string</code> 开始搜索的位置。
      </p>
      <p class="para">
       如果给出的 <code class="parameter">offset</code> 是非负数，然后 <span class="function"><strong>strcspn()</strong></span> 将会从 <code class="parameter">string</code> 的
       <code class="parameter">offset</code> 位置开始检查字符串。例如。在字符串“<code class="literal">abcdef</code>”中，位置为 <code class="literal">0</code>
       的字符是“<code class="literal">a</code>”，位置为 <code class="literal">2</code> 的字符是“<code class="literal">c</code>”，等等。
      </p>
      <p class="para">
       如果给出的 <code class="parameter">offset</code> 是负数，则 <span class="function"><strong>strcspn()</strong></span> 将会从距离 <code class="parameter">string</code> 末尾的第
       <code class="parameter">offset</code> 个位置开始检查字符串。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       要检查的部分 <code class="parameter">string</code> 的长度。
      </p>
      <p class="para">
       如果给出的 <code class="parameter">length</code> 是非负数，然后将检查 <code class="parameter">string</code> 中起始位置后的 <code class="parameter">length</code> 字符。
      </p>
      <p class="para">
       If <code class="parameter">length</code> is given and is negative,
       then <code class="parameter">string</code> will be examined from the
       starting position up to <code class="parameter">length</code>
       characters from the end of <code class="parameter">string</code>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.strcspn-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the length of the initial segment of <code class="parameter">string</code>
   which consists entirely of characters <em class="emphasis">not</em> in <code class="parameter">characters</code>.
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    When a <code class="parameter">offset</code> parameter is set, the returned length
    is counted starting from this position, not from the beginning of
    <code class="parameter">string</code>.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.strcspn-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">length</code> 现在允许为 null。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.strcspn-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="strcspn.example">
   <p><strong>示例 #1 <span class="function"><strong>strcspn()</strong></span> 示例</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strcspn</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcd'</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strcspn</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcd'</span><span style="color: #007700">,&nbsp;&nbsp;</span><span style="color: #DD0000">'banana'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strcspn</span><span style="color: #007700">(</span><span style="color: #DD0000">'hello'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'l'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$d&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strcspn</span><span style="color: #007700">(</span><span style="color: #DD0000">'hello'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'world'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$e&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strcspn</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcdhelloabcd'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'abcd'</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">9</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$f&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strcspn</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcdhelloabcd'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'abcd'</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">9</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$e</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$f</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
int(0)
int(0)
int(2)
int(2)
int(5)
int(4)
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 notes" id="refsect1-function.strcspn-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">此函数可安全用于二进制对象。</span></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.strcspn-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.strspn.html" class="function" rel="rdfs-seeAlso">strspn()</a> - 计算字符串中全部字符都存在于指定字符集合中的第一段子串的长度</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>